home *** CD-ROM | disk | FTP | other *** search
- Path: rcp6.elan.af.mil!rscernix!danpop
- From: danpop@mail.cern.ch (Dan Pop)
- Newsgroups: comp.lang.c
- Subject: Re: Help : Printf() format %E !
- Date: 23 Feb 96 12:12:55 GMT
- Organization: CERN European Lab for Particle Physics
- Message-ID: <danpop.825077575@rscernix>
- References: <4g18nf$jps@ias2.ichange.com> <danpop.824495447@rscernix> <312C786F.6696@oc.com>
- NNTP-Posting-Host: ues5.cern.ch
- X-Newsreader: NN version 6.5.0 #7 (NOV)
-
- In <312C786F.6696@oc.com> Larry Weiss <lfw@oc.com> writes:
-
- >Dan Pop wrote:
- > >
- > > In <4g18nf$jps@ias2.ichange.com> akmp@mindware.soft.net (anil kumar m p) writes:
- > > >i am involved converting programs written in Fortran onto C.
- > > >one problem that i am unable to solve is printing exponential
- > > >values in C.
- > > >Fortran prints exponential values with 0.4567E02
- > > >whereas C prints the same as 4.5670E01.
- > > >How do i make C print in 0.somethingEsomething format.
- >
- >> The %E format cannot do what you need, by definition. So, you have to
- >> split the number into mantissa and exponent and printf them separately.
- >> You can do this either mathematically or by playing with stdio functions:
- >> double mantissa;
- >> int exponent;
- >> sprintf(buff, "%E", 0.4567E02);
- >> *strchr(buff, 'E') = ' ';
- >> sscanf(buff, "%lf %d", &mantissa, &exponent);
- >> mantissa /= 10;
- >> if (mantissa != 0) exponent++;
- >> else exponent = 0;
- >> printf("%fE%+.02d\n", mantissa, exponent);
- >>
- >> If mathematical functions are inexpensive on your system, splitting
- >> the number into mantissa and exponent with log10 and pow might be
- >> more efficient.
- >
- >Or, use sprintf() to get a string containing the components, and
- >then use character manipulations to adjust the formatting.
-
- Incrementing the exponent using character manipulations is not exactly
- straightforward (it isn't rocket science, either) :-)
-
- Dan
- --
- Dan Pop
- CERN, CN Division
- Email: danpop@mail.cern.ch
- Mail: CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
-